package com.company.test29;

import java.util.StringJoiner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by v on 16-11-29.
 */
public class Test291 {
    private static Lock lock=new ReentrantLock(true);
    private static Condition condition=lock.newCondition();
    private static String string="";

    static class PrintB implements Runnable{

        @Override
        public void run() {
            lock.lock();
            try {
                string+="b ";
                condition.await();
                for(int i=0;i<19;i++){
                    string+="b ";
                }
                condition.signalAll();
                condition.await();
                for(int i=0;i<80;i++){
                    string+="b ";
                }
            }catch (InterruptedException ex){
                ex.printStackTrace();
            }finally {
                condition.signalAll();
                lock.unlock();
            }
        }
    }

    static class PrintA implements Runnable{
        @Override
        public void run() {
            lock.lock();
            for(int i=0;i<100;i++){
                string+="a ";
            }
            condition.signalAll();
            lock.unlock();
        }
    }

    static class PrintNumber implements Runnable{
        int number;

        public PrintNumber(int number){
            this.number=number;
        }

        @Override
        public void run() {
            lock.lock();
            string+=1+" ";
            try {
                condition.await();
                condition.await();
                for(int i=2;i<90;i++){
                    string+=i+" ";
                }
                condition.signalAll();
                condition.await();
                for(int i=90;i<=100;i++){
                    string+=i+" ";
                }
            }catch (InterruptedException ex){
                ex.printStackTrace();
            }finally {
                condition.signalAll();
                lock.unlock();
            }
        }
    }

    public static void main(String[] args) {
        String answer="1 b a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a b b b b b b b b b b b b b b b b b b b 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b 90 91 92 93 94 95 96 97 98 99 100 ";

        while (true){
            string="";
            PrintA printa=new PrintA();
            PrintB printb=new PrintB();
            PrintNumber printNumber=new PrintNumber(100);

            Thread thread1=new Thread(printa);
            Thread thread2=new Thread(printb);
            Thread thread3=new Thread(printNumber);

            try {
                thread3.start();
                Thread.sleep(10);
                thread2.start();
                Thread.sleep(10);
                thread1.start();
            }catch (InterruptedException ex){
                ex.printStackTrace();
            }

            try {
                Thread.sleep(100);
            }catch (InterruptedException ex){
                ex.printStackTrace();
            }

            System.out.println(string.equals(answer));
        }
    }
}